package xyz.syyrjx.blog.util;

/**
 * @author 大地崩坏苍蝇兽
 * @date 2024/2/24 12:44
 * @description ...
 * @since v0.0.1
 */
public class StringUtil {

    /**
     * 计算字符串中某个字符串出现的次数
     * @param str
     * @param target
     * @return
     */
    public static int countSubStr(StringBuilder str, String target) {
        if (str == null || target == null) {
            return 0;
        }
        int step = target.length();
        int max = str.length();
        int count = 0;
        int lastIndex = 0;
        while (lastIndex < max && (lastIndex = str.indexOf(target, lastIndex)) != -1) {
            lastIndex += step;
            count++;
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(countSubStr(new StringBuilder("当然"), ConstValuePool.MARKDOWN_CODE_BLOCK_START));
    }

    public static String searchDeal(String search) {
        if(search != null && search.length() > 0){
            if (!search.startsWith("%")) {
                search = "%" + search;
            }
            if (!search.endsWith("%")){
                search = search + "%";
            }
        }else {
            search = null;
        }
        return search;
    }

}
